<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>验证码使用示例</title>
<style type="text/css">
 img {vertical-align:top;}
 a{font-size:12px; color:#999; text-decoration:none;}
</style>
</head>
<?php
  //开启session
session_start();
//判断是否有表单提交
if(!empty($_POST)){
//获取用户输入的验证码字符串
$code = isset($_POST['verify_code']) ? trim($_POST['verify_code']) : '';
//判断SESSION中是否存在验证码
if(empty($_SESSION['verify_code'])){
	die('输入验证码时间过长。');
}
//将字符串都转成小写然后再进行比较
if (strtolower($code) == strtolower($_SESSION['verify_code'])){
	 echo ("<script>alert('验证码正确')</script>");	 
} else{
	 echo ("<script>alert('验证码输入错误')</script>");
}
unset($_SESSION['verify_code']); //清除SESSION数据
}
?>
<body>
 <form action="" method="post"> 
   验证码:<input type="text" name="verify_code"> 
  <img src="../common/code.php" alt="" id="code_img"/><a href="#" id="change">看不清，换一张</a>
  <br>
  <input type="submit" value="验证">
</form>
<script>
	var change = document.getElementById("change");
	var img = document.getElementById("code_img");
	change.onclick = function(){
		img.src = "../common/code.php?t="+Math.random(); //增加一个随机参数，防止图片缓存
		return false; //阻止超链接的跳转动作
	}
</script>
</body>
</html>